package com.wy.recyclerview;

import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wy.recyclerview.commonRecyclerAdapter.MyAdapter1;
import com.wy.recyclerview.decoration.CategoryItemDecoration;
import com.wy.recyclerview.decoration.Fruit;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {


    private ConstraintLayout root;
    private RecyclerView recyclerView;

    private ArrayList<Fruit> fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        root = findViewById(R.id.root);
        recyclerView = findViewById(R.id.recyclerView);

        initData();
//        MyAdapter myAdapter = new MyAdapter(this, fruitList);
        MyAdapter1 myAdapter = new MyAdapter1(this, fruitList);
        //网格
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new CategoryItemDecoration(R.color.purple_500));
        recyclerView.setAdapter(myAdapter);

        //添加头部
//        View viewHeader = LayoutInflater.from(this).inflate(R.layout.item_main_header, root, false);
//        recyclerView.addHeaderView(viewHeader);

        // 实现左边侧滑删除 和 拖动排序
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                // 获取触摸响应的方向   包含两个 1.拖动dragFlags 2.侧滑删除swipeFlags
                // 代表只能是向左侧滑删除，当前可以是这样ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT
                int swipeFlags = ItemTouchHelper.LEFT;


                // 拖动
                int dragFlags = 0;
                if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    // GridView 样式四个方向都可以
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.LEFT |
                            ItemTouchHelper.DOWN | ItemTouchHelper.RIGHT;
                } else {
                    // ListView 样式不支持左右，只支持上下
                    dragFlags = ItemTouchHelper.UP |
                            ItemTouchHelper.DOWN;
                }

                return makeMovementFlags(dragFlags, swipeFlags);
            }

            /**
             * 拖动的时候不断的回调方法
             */
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                // 获取原来的位置
                int fromPosition = viewHolder.getAdapterPosition();
                // 得到目标的位置
                int targetPosition = target.getAdapterPosition();
                if (fromPosition > targetPosition) {
                    for (int i = fromPosition; i < targetPosition; i++) {
                        Collections.swap(fruitList, i, i + 1);// 改变实际的数据集
                    }
                } else {
                    for (int i = fromPosition; i > targetPosition; i--) {
                        Collections.swap(fruitList, i, i - 1);// 改变实际的数据集
                    }
                }
                myAdapter.notifyItemMoved(fromPosition, targetPosition);
                return true;
            }

            /**
             * 侧滑删除后会回调的方法
             */
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                // 获取当前删除的位置
                int position = viewHolder.getAdapterPosition();
                fruitList.remove(position);
                // adapter 更新notify当前位置删除
                myAdapter.notifyItemRemoved(position);
            }

            /**
             * 拖动选择状态改变回调
             */
            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                    // ItemTouchHelper.ACTION_STATE_IDLE 看看源码解释就能理解了
                    // 侧滑或者拖动的时候背景设置为灰色
                    viewHolder.itemView.setBackgroundColor(Color.GRAY);
                }
            }

            /**
             * 回到正常状态的时候回调
             */
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                // 正常默认状态下背景恢复默认
                viewHolder.itemView.setBackgroundColor(0);
                ViewCompat.setTranslationX(viewHolder.itemView, 0);
            }
        });
        // 这个就不多解释了，就这么attach
        itemTouchHelper.attachToRecyclerView(recyclerView);


    }

    private void initData() {
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "香蕉"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "苹果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "橘子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "火龙果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "西瓜"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "桃子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "香蕉"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "苹果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "橘子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "火龙果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "西瓜"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "桃子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "香蕉"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "苹果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "橘子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "火龙果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "西瓜"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "桃子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "香蕉"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "苹果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "橘子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "火龙果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "西瓜"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "桃子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "香蕉"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "苹果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "橘子"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "火龙果"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "西瓜"));
        fruitList.add(new Fruit(R.mipmap.ic_launcher, "桃子"));
    }

}